using System;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;
using SecondLevelCaching.Data;
using SecondLevelCaching.Data.Models;

namespace SecondLevelCachingExample
{
    /// <summary>
    /// Registers the services used by application with the container
    /// </summary>
    public class ServiceInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            var connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
            if (string.IsNullOrEmpty(connectionString))
                throw new Exception("The connection string for Northwind could not be found in the configuration, please make sure you have set this");

            container.Register(
                Component.For<SecondLevelCaching.Data.IDbContext>()
                    .ImplementedBy<SecondLevelCaching.Data.Models.NorthwindContext>()
                    .LifeStyle.PerWebRequest
                    .Named("NorthwindCachedContext")
                    .DependsOn(
                        Parameter.ForKey("nameOrConnectionString").Eq("name=Northwind"))
                );
             
        }
    }
}